package display
{
	import flash.events.Event;
	import flash.geom.Point;
	
	import mx.core.FlexSprite;
	import mx.graphics.IStroke;
	
	/**
	 * 两点决定一条线，该线的绘制可以改变stroke，并且支持点
	 * 的及时修改
	 */
	public class Line extends FlexSprite
	{
		private var _stroke:IStroke;
		
		private var _pointOne:Point;
		
		private var _pointTwo:Point;
		
		public static var  scaledPoints:Array = [];
		
		public function Line(points:Array, stroke:IStroke)
		{
			super();
			this.pointOne = points[0] as Point;
			this.pointTwo = points[1] as Point;
			this.stroke = stroke;
			validate();
			this.addEventListener(Event.ADDED_TO_STAGE, onStageReady);
		}
		
		private function onStageReady(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, onStageReady);
			//stageInit();
		}
		
		public function validate():void
		{
			if(pointOne && pointOne && stroke)
			{
				draw();
			}
		}
		
		private function draw():void
		{
			this.graphics.clear();
			stroke.apply(this.graphics);
			this.graphics.moveTo(pointOne.x, pointOne.y);
			this.graphics.lineTo(pointTwo.x , pointTwo.y);
		}
		
		/**
		 * 拆分点是否在该line上，如果是，则返回拆分点。
		 * 否则返回方位
		 * lt:表示上方或者左边
		 * rb:表示下方或者右边
		 */
		public function split(position:Number, direction:String):Object
		{
			var result:Object;
			switch(direction)
			{
				case "horizontal":
				    if(pointOne.x < position && pointTwo.x > position)
				    {
				    	result = getPoint(position, 'x');
				    }
				    else if(pointOne.x == position)
				    {
				    	result = pointTwo.x > position ? "rb" : "lt";
				    }
				    else if(pointOne.x > position)
				    {
				    	result =  "rb";
				    }
				    else if(pointOne.x < position)
				    {
				    	result =  "lt";
				    }
				    break;
				case "vertical":
				 	if(pointOne.y < position && pointTwo.y > position)
				    {
				    	result = getPoint(position, 'y');
				    }
				    else if(pointOne.y == position)
				    {
				    	result = pointTwo.y > position ? "rb" : "lt";
				    }
				    else if(pointOne.y > position)
				    {
				    	result =  "rb";
				    }
				    else if(pointOne.y < position)
				    {
				    	result =  "lt";
				    }
				    break;
			}
			return result;
		}
		
		/**
		 * 根据x,或者y得到该线上的点
		 */
		public function getPoint(position:Number, axis:String):Point
		{
		   var k:Number = (pointTwo.y - pointOne.y) / (pointTwo.x - pointOne.x);
		   var b:Number = pointOne.y  - k * pointOne.x;
		   
		   var p:Point = new Point();
		   if(axis == "x")
		   {
		   	  p.x = position;
		  	  p.y = k * position + b;
		   }
		   else
		   {
		   	  p.x = k == Infinity ? (pointTwo.x) : (position - b) / k;
		   	  p.y = position;
		   }
		   trace("split point at: " + p);
		   return p;
		}
		

		public function get pointTwo():Point
		{
			return _pointTwo;
		}

		public function set pointTwo(v:Point):void
		{
			_pointTwo = v;
			validate();
		}

		public function get pointOne():Point
		{
			return _pointOne;
		}

		public function set pointOne(v:Point):void
		{
			_pointOne = v;
			validate();
		}
		
		public function scale(oldSize:Point, newSize:Point):void
		{
			if(scaledPoints.indexOf(pointOne) < 0)
			{
				scalePoint(pointOne, "x", oldSize, newSize);
				scalePoint(pointOne, "y", oldSize, newSize);
				scaledPoints.push(pointOne);
			}
			if(scaledPoints.indexOf(pointTwo) < 0)
			{
				scalePoint(pointTwo, "x", oldSize, newSize);
				scalePoint(pointTwo, "y", oldSize, newSize);
				scaledPoints.push(pointTwo);
			}			
		}
		
		private function scalePoint(p:Point, axis:String, oldSize:Point, newSize:Point):void
		{
			var scale:Number = newSize[axis] / oldSize[axis];
			trace(axis + " scale: " + scale + " old: " + p[axis] + " 0ld size: " + oldSize[axis]
			 + " new size: " + newSize[axis]);
			if(Math.round(p[axis]) == Math.round(oldSize[axis]))
			{
				p[axis] = newSize[axis];
			}
			else
			{
				p[axis] = Math.max(0, scale * p[axis]);
			}
			trace("p[axis]: " + p[axis]);
		}

		public function get stroke():IStroke
		{
			return _stroke;
		}

		public function set stroke(v:IStroke):void
		{
			_stroke = v;
			validate();
		}
	}
}